package com.edu.web.queryanswer;

import com.edu.entity.queryanswer.Answer;
import com.edu.entity.queryanswer.AnswerVo;
import com.edu.entity.queryanswer.Query;
import com.edu.entity.queryanswer.Querysc;
import com.edu.entity.user.EduUser;
import com.edu.service.queryanswer.AnswerService;
import com.edu.service.queryanswer.QueryService;
import com.edu.service.user.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import java.util.Date;
import java.util.List;

@Controller
public class QueryAnswerController {
    @Autowired
    private QueryService queryService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private UserService userService;
    @RequestMapping("/addquery")
    public String addquery(){
        return "queryanswer/add";
    }


    @RequestMapping("/queryByName")
    public String queryByName(Model model,String queryname,Integer page,Integer size,Principal principal){
        if (page==null||page==0) {
            page = 1;
        }
        if(queryname == null){
            queryname = "";
        }
        EduUser eduUser=userService.findUserByName(principal.getName());
        Integer count=queryService.selecttotal(queryname);
        List<Query> list = queryService.findQueryByName(queryname,page,size);
        for (Query query : list){
            query.setQueryusername(userService.findUserById(query.getQueryuserid()).getUsername());
        }
        System.out.println("count:"+count);
        model.addAttribute("userinfo",eduUser);
        model.addAttribute("count",count);
        model.addAttribute("curr",page);
        model.addAttribute("querylist",list);
        model.addAttribute("queryname",queryname);
        return "queryanswer/index";
    }
    @RequestMapping("/newquery")
    public String newquery(Query query,Principal principal){
        Date date=new Date();
        query.setCreatetime(date);
        EduUser eduUser=userService.findUserByName(principal.getName());
        query.setQueryuserid(eduUser.getId());
        queryService.insertquery(query);
        return "redirect:queryByName";
    }

    @RequestMapping("/answerdetail")
    public String answerdetail(Model model,Integer id){
        Query query=queryService.findById(id);
        queryService.broswerin(id);
        EduUser eduUser=userService.findUserById(query.getQueryuserid());
        query.setQueryusername(eduUser.getUsername());
        List<AnswerVo> answerList=answerService.selectAnswer(id);
        System.out.println(answerList);
        for (AnswerVo answervo: answerList){
            System.out.println(userService.findUserById(answervo.getUserId()));
            answervo.setUsername(userService.findUserById(answervo.getUserId()).getUsername());
        }
        model.addAttribute("query",query);
        model.addAttribute("answerLists",answerList);
        return "queryanswer/detail";
    }

    @RequestMapping("/addanswer")
    public String addanswer(Answer answer,Principal principal){
        EduUser eduUser=userService.findUserByName(principal.getName());
        answer.setUserId(eduUser.getId());
        Date date=new Date();
        answer.setAnswerTime(date);
        answerService.insertanswer(answer);
        return "redirect:answerdetail?id="+answer.getQueryId();
    }

    @RequestMapping("/honor")
    public String honor(){
        return "queryanswer/honor";
    }

    @RequestMapping("/zan")
    @ResponseBody
    public String zan(@RequestParam("id") Integer id){
        answerService.insertzan(id);
        System.out.println("zan"+id);
        return "success";
    }

//    @RequestMapping("/reslovelist")
//    public String reslovelist(Model model,int isslove){
//        List<Query> queryList=queryService.reslovequery(isslove);
//        model.addAttribute("reslovelist",queryList);
//        System.out.println("45125"+queryList);
//        return "queryanswer/reslovepage";
//    }
    @RequestMapping("/caina")
    @ResponseBody
    public String caina(@RequestParam("id") Integer id){
        answerService.caina(id);
        System.out.println("id"+id);
        return "666";
    }

    //    收藏
    @ResponseBody
    @RequestMapping("/collectquery")
    public String select(Principal principal, Integer id){
        Integer prinId=queryService.findUserByPrin(principal.getName()).getId();
        Query query=queryService.findById(id);
        Date date=new Date();
        Querysc querysc=new Querysc(query.getId(),prinId,query.getQueryuserid(),query.getQuerytitle(),date);
        queryService.insertCollect(querysc);
        return "1";
    }

    @RequestMapping("/reslove")
    public String reslove(Model model,String queryname,Integer page,Integer size){
        if (page==null||page==0) {
            page = 1;
        }
        if(queryname == null){
            queryname = "";
        }
        Integer count=queryService.gettotal(queryname);
        List<Query> querys=queryService.reslovequery(page,size);
        model.addAttribute("count",count);
        model.addAttribute("curr",page);
        model.addAttribute("querys",querys);
        System.out.println("querys:"+querys);
        return "queryanswer/reslovepage";
    }
}
